<?php
// +----------------------------------------------------------------------
// | 07FLYCRM [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\ten\controller;

use think\db;

/**
 * 菜单控制器
 */
class TenMenu extends TenBase
{
    public function menus()
    {
        $menu_select = $this->logicTenMenu->getAllList();
        $menu_select = array_unique($menu_select);
        $string = "";
        foreach ($menu_select as $menu) {
            $string .= "'" . $menu . "'=>'" . $menu . "',<br>";
        }
        echo $string;
    }

    /**
     * 菜单列表
     */
    public function show()
    {
        if (!empty($this->param['pid'])) {
            $pid = $this->logicTenMenu->getTenMenuAllPidPath(29);
            echo $pid;
        }
        return $this->fetch('show');
    }

    /**
     * 菜单列表
     */
    public function show_json()
    {
        $where = $this->logicTenMenu->getWhere($this->param);
        $list = $this->logicTenMenu->getTenMenuList($where, true, 'sort asc', DB_LIST_ROWS);
        return $list;
    }

    /**
     * 菜单列表
     */
    public function get_list_tree()
    {
        $where = $this->logicTenMenu->getWhere($this->param);
        $tree = $this->logicTenMenu->getTenMenuListTree($where);
        return $tree;
    }

    public function get_menu_path()
    {
        if (!empty($this->param['pid'])) {
            return $this->logicTenMenu->getTenMenuAllPidPath($this->param['pid']);
        } else {
            return $this->logicTenMenu->getTenMenuAllPidPath(0);
        }
    }


    /**
     * 获取菜单Select结构数据
     */
    public function getMenuSelectData($oid, $sid)
    {
        $menu_select = $this->logicTenMenu->menuToSelect($oid, $sid);
        $this->assign('menu_select', $menu_select);
    }

    /**
     * 菜单添加
     */
    public function add()
    {
        IS_POST && $this->jump($this->logicTenMenu->tenMenuAdd($this->param));
        //获取菜单Select结构数据
        $where = $this->logicTenMenu->getWhere($this->param);
        $menu_select = $this->logicTenMenu->getTenMenuTreeSelect($where);
        $this->assign('menu_select', $menu_select);
        if (!empty($this->param['id'])) {
            $this->assign('pid', $this->param['id']);
        } else {
            $this->assign('pid', '0');
        }
        return $this->fetch('add');
    }

    /**
     * 菜单编辑
     */
    public function edit()
    {
        IS_POST && $this->jump($this->logicTenMenu->tenMenuEdit($this->param));

        $info = $this->logicTenMenu->getTenMenuInfo(['id' => $this->param['id']]);
        //获取菜单Select结构数据
        $where = $this->logicTenMenu->getWhere($this->param);
        $menu_select = $this->logicTenMenu->getTenMenuTreeSelect($where);

        $this->assign('menu_select', $menu_select);
        $this->assign('info', $info);

        return $this->fetch('edit');
    }

    /**
     * 数据状态设置
     */
    public function del()
    {
        $this->jump($this->logicTenMenu->tenMenuDel($this->param));
    }

    /**
     * 菜单添加
     */
    public function move()
    {
        IS_POST && $this->jump($this->logicTenMenu->tenMenuMove($this->param));

        //获取菜单Select结构数据
        $where = $this->logicTenMenu->getWhere($this->param);
        $menu_select = $this->logicTenMenu->getTenMenuTreeSelect($where);
        $this->assign('menu_select', $menu_select);
        if (!empty($this->param['id'])) {
            $this->assign('id', $this->param['id']);
        } else {
            $this->assign('id', '0');
        }
        return $this->fetch('move');
    }

    /**
     * 菜单复制
     */
    public function copy()
    {
        IS_POST && $this->jump($this->logicTenMenu->tenMenuCopy($this->param));

        //获取菜单Select结构数据
        $where = $this->logicTenMenu->getWhere($this->param);
        $menu_select = $this->logicTenMenu->getTenMenuTreeSelect($where);
        $this->assign('menu_select', $menu_select);

        if (!empty($this->param['id'])) {
            $this->assign('id', $this->param['id']);
        } else {
            $this->assign('id', '0');
        }
        return $this->fetch('copy');
    }

    /**
     * 菜单复制
     */
    public function copy_one()
    {
        $this->jump($this->logicTenMenu->tenMenuCopyOne($this->param));
    }

    /**
     * 启用
     */
    public function set_visible()
    {
        $this->jump($this->logicTenBase->setField('TenMenu', $this->param));
    }

    /**
     * 排序
     */
    public function set_sort()
    {
        $this->jump($this->logicTenBase->setSort('TenMenu', $this->param));
    }

    /**
     * 字段修改
     */
    public function set_field()
    {
        $this->jump($this->logicTenBase->setField('TenMenu', $this->param));
    }

    /**
     * 上传
     */
    public function import()
    {
        $uploadfilefather = 'clearance';//上级文件目录
        $uploadfilepath = 'ten_user_' . SYS_USER_ID;//上传文件目录
        $uploadtarget = url('TenMenu/import', array('ajaxmodel' => 'upload', 'uploadfilepath' => $uploadfilepath));
        $this->param['uploadfilefather'] = $uploadfilefather;
        $this->param['uploadfilepath'] = $uploadfilepath;
        if (!empty($this->param['ajaxmodel'])) {
            switch ($this->param['ajaxmodel']) {
                case 'get':
                    return $this->logicUpload->getUploadFile($this->param);
                    break;
                case 'del':
                    return $this->jump($this->logicUpload->delUploadFile($this->param));
                    break;
                case 'upload':
                    return $this->jump($this->logicUpload->uploadFile($this->param));
                    break;
                case 'import':
                    return $this->jump($this->logicTenMenu->tenMenuImportFile($this->param));
                    break;
            }
        }
        $this->assign('uploadfilepath', $uploadfilepath);
        $this->assign('ajaxtarget', url());//执行地址
        $this->assign('uploadtarget', $uploadtarget);//上传地址
        return $this->fetch('upload/upload');
    }

    //导出
    public function export()
    {
        if (IS_POST) {
            $modulename = $this->param['modulename'];
            $this->jump($this->logicTenMenu->tenMenuExport($modulename));
        }
        // 所有模块
        $module_select_list = $this->logicTenModule->getTenModuleList([], true, 'sort asc', false);
        $this->assign('module_select_list', $module_select_list);
        return $this->fetch('export');
    }

}
